package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Tag;
import com.itheima.ydd.service.TagService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XGC
 * @description: TODO 标签管理
 * @date 2022/03/02 16:09
 * @title: TagController
 * @projectName hmzx_ydd
 */
@RestController
@RequestMapping("/ydd_tag")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * @Author XGC
     * @Description //TODO 标签管理-分页查询
     * @Date 16:12 2022/03/02
     * @Param [tagname, page, limit]
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/list")
    public ResultData list(String tagname, int page, int limit){

        Page<Tag> tagPage = new Page<>(page, limit);

        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(tagname),Tag::getTagname,tagname);

        tagService.page(tagPage,lqw);


        return ResultData.ok("", PageResult.init(tagPage.getTotal()
                ,tagPage.getSize()
                ,tagPage.getCurrent()
                ,tagPage.getRecords()));
    }

   /**
    * @Author XGC
    * @Description //TODO 标签管理-添加标签
    * @Date 16:37 2022/03/02
    * @Param [tag]
    * @return com.itheima.ydd.common.ResultData
    */
    @PostMapping("/add")
    public ResultData add(@RequestBody Tag tag){
        tag.setDefault_data(1);

        tagService.save(tag);
        return ResultData.ok("","标签添加成功！");
    }

    /**
     * @Author XGC
     * @Description //TODO 标签管理-根据Id查询标签信息
     * @Date 16:45 2022/03/02
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id){
        Tag tag = tagService.getById(id);
        return ResultData.ok("",tag);
    }

    /**
     * @Author XGC
     * @Description //TODO 标签管理-删除标签
     * @Date 16:52 2022/03/02
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/del")
    public ResultData delete(Long id){

        tagService.removeById(id);
        return ResultData.ok("","标签删除成功！");
    }

    /**
     * @Author XGC
     * @Description //TODO 标签管理-修改标签信息
     * @Date 17:05 2022/03/02
     * @Param [tag]
     * @return com.itheima.ydd.common.ResultData
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Tag tag){

        tagService.updateById(tag);
        return ResultData.ok("","标签修改成功");
    }

    /**
     * @Author XGC
     * @Description //TODO 标签管理-查询所有标签信息
     * @Date 17:14 2022/03/02
     * @Param []
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/all")
    public ResultData all(){
        List<Tag> list = tagService.list();
        return ResultData.ok("",list);
    }
}
